<?php

namespace app\admin\validate;

use think\Validate;
use app\common\Error;

class Activity extends Validate
{
    protected $rule = [
        'id' => 'require',
        'name' => 'require',
        'cover_img' => 'require',
        'type' => 'require|inType',
        'scene' => 'require|array',
        'recharge_price' => 'require|float',
        'send_currency' => 'require|integer',
        'applet_ids' => 'require',
        'status' => 'require|inStatus',
        'start_at' => 'require|dateFormat:Y-m-d H:i:s',
        'end_at' => 'require|dateFormat:Y-m-d H:i:s',
    ];

    protected $message = [
        'id.require' => 'id不能为空',
        'name.require' => '活动名称不能为空',
        'cover_img.require' => '活动图片不能为空',
        'type.require' => '活动类型不能为空',
        'type.inType' => '活动类型不正确',
        'scene.require' => '场景不能为空',
        'scene.array' => '场景参数不正确',
        'recharge_price.require' => '充值金额不能为空',
        'send_currency.require' => '赠送金额不能为空，不赠送可以填0',
        'select.require' => '小程序必选',
        'status.require' => '状态不能为空',
        'start_at.require' => '开始时间不能为空',
        'start_at.datetime' => '开始时间的时间格式不正确',
        'end_at.require' => '结束时间不能为空',
        'end_at.datetime' => '结束时间的时间格式不正确',
    ];

    protected $scene = [
        'add' => ['name', 'cover_img', 'type', 'scene', 'recharge_price', 'send_currency', 'applet_ids', 'status', 'start_at', 'end_at'],
        'edit' => ['id', 'name', 'cover_img', 'type', 'scene', 'recharge_price', 'send_currency', 'applet_ids', 'status', 'start_at', 'end_at'],
        'delete' => ['id'],
        'detail' => ['id'],
    ];

    protected function inStatus($value, $rule, $data): bool
    {
        return in_array($value,array_keys(\app\model\Activity::STATUS_ARR));
    }

    protected function inType($value, $rule, $data): bool
    {
        return in_array($value,array_keys(\app\model\Activity::TYPE_ARR));
    }
}